package com.milhouz.quizzdroid.view;

import java.io.IOException;
import java.util.Random;

import android.content.Context;
import android.graphics.drawable.Drawable;

import com.milhouz.quizzdroid.R;

public class AvatarUtil {

	private static final Random RANDOM = new Random();

	private AvatarUtil() {

	}

	public static Drawable getAvatarFromStats(Context parent, float nbResponse, float totalQuestions) {
		float stats = 0.5f;
		if (totalQuestions > 0) {
			stats = nbResponse / totalQuestions;
		}
		try {
			if (stats == 0) {
				return Drawable.createFromStream(parent.getAssets().open("smileys/Black/bad_4.png"), null);
			} else if (stats >= 0.75 && stats < 1) {
				return Drawable.createFromStream(parent.getAssets().open("smileys/Black/good_5.png"), null);
			} else if (stats < 0.75 && stats > 0.5) {
				return Drawable.createFromStream(parent.getAssets().open("smileys/Black/good_2.png"), null);
			} else if (stats == 0.5) {
				return Drawable.createFromStream(parent.getAssets().open("smileys/Black/normal.png"), null);
			} else if (stats < 0.5 && stats >= 0.25) {
				return Drawable.createFromStream(parent.getAssets().open("smileys/Black/bad_1.png"), null);
			} else if (stats < 0.25 && stats > 0) {
				return Drawable.createFromStream(parent.getAssets().open("smileys/Black/bad_5.png"), null);
			} else if (stats == 1) {
				return Drawable.createFromStream(parent.getAssets().open("smileys/Black/win.png"), null);
			}
		} catch (IOException e) {
		}
		return parent.getResources().getDrawable(R.drawable.icon);
	}

	public static Drawable getWhatAvatar(Context parent) {
		try {
			return Drawable.createFromStream(parent.getAssets().open("smileys/Black/what.png"), null);
		} catch (IOException e) {
		}
		return parent.getResources().getDrawable(R.drawable.icon);
	}

	public static Drawable getAvatarByResponse(Context parent, boolean isGoodResponse) {
		int imageNumber = RANDOM.nextInt(4) + 1;
		try {
			if (isGoodResponse) {
				return Drawable.createFromStream(parent.getAssets().open("smileys/Black/good_" + imageNumber + ".png"), null);
			} else {
				return Drawable.createFromStream(parent.getAssets().open("smileys/Black/bad_" + imageNumber + ".png"), null);
			}
		} catch (IOException e) {
		}
		return getWhatAvatar(parent);
	}
}
